import qs from './qs'
var baseUrl = 'http://bufantec.com';
var obj = {
  // get请求
  get(url, data) {
    return new Promise( (resole, reject) => {
      wx.request({
        url: baseUrl + url,
        method: 'GET',
        data,
        success(res) {
          resole(res)
        },
        fail(res) {
          reject(res)
        }
      })
    } )
  },
  // post请求
  post(url, data) {
    return new Promise( (resole, reject) => {
      wx.request({
        url: baseUrl + url,
        method: 'POST',
        header: {
          'content-type':"application/x-www-form-urlencoded"
        },
        data: qs.stringify(data, { allowDots: true}),
        success(res) {
          resole(res)
        },
        fail(res) {
          reject(res)
        }
      })
    } )
  }
}
// 判断是何种请求
function http(data) {
  if (data.method) {
    return obj.post(data.url, data.data)
  } else {
    return obj.get(data.url, data.data)
  }
}
// 暴露接口
export {
  http
}